package com.dolphin.common.component.var.enums;

import lombok.Getter;
import lombok.ToString;
import org.apache.commons.lang3.StringUtils;

import java.util.Objects;

/**
 * @author shida
 */
@Getter
@ToString
public enum VarTypeEnum {
    BIZ_VAR(0, "ORIGINAL", "原始变量", "b", ""),
    TIME_WINDOW_VAR(1, "STATISTICS", "统计变量", "t", "#"),
    SCORE_CARD_VAR(2, "SCORE_CARD", "评分卡变量", "c", "#"),
    EXT_FUNCTION_VAR(3, "FUNCTION", "函数变量", "f", "#"),
    EXT_SCRIPT_VAR(5, "SCRIPT", "脚本变量", "s", "#"),
    TIME_WINDOW_VAR_EXT_OFFLINE(6, "OFFLINESTAT", "离线统计变量", "o", "#"),
    RULE_OUTPUT_VAR(7, "RULE_OUTPUT", "策略变量", "r", ""),
    UDF_VAR(8, "UDF", "UDF变量", "u", ""),
    UDF_DERIVED_VAR(9, "UDF_DERIVED", "UDF变量", "d", ""),
    GLOBAL_VAR(10, "GLOBAL", "全局变量", "g", ""),
    GLOBAL_REF_VAR(11, "GLOBAL_REF", "全局引用变量", "G", "")
    ;


    private int id;
    private String code;
    private String description;
    private String index;
    private String sign;

    VarTypeEnum(int id, String code, String description, String index, String sign) {
        this.id = id;
        this.code = code;
        this.description = description;
        this.index = index;
        this.sign = sign;
    }

    public static VarTypeEnum fromCode(String code){
        if(StringUtils.isEmpty(code)){
            return null;
        }

        for(VarTypeEnum item : values()){
            if(Objects.equals(item.getCode(), code.toUpperCase())){
                return item;
            }
        }

        return null;
    }

    public static VarTypeEnum fromIndex(String index){
        if(StringUtils.isEmpty(index)){
            return null;
        }

        for (VarTypeEnum item : values()){
            if(Objects.equals(item.getIndex(), index)){
                return item;
            }
        }

        return null;
    }

    public static boolean isBid(String index){
        if(StringUtils.isEmpty(index)){
            return false;
        }

        for (VarTypeEnum item : values()){
            if(Objects.equals(item.getIndex(), index)){
                return true;
            }
        }

        return false;
    }

    public boolean isUdfVar(){
        return this == UDF_VAR;
    }

    public boolean isUdfDerivedVar(){
        return this == UDF_DERIVED_VAR;
    }

    public boolean isExtVar(){
        return this == RULE_OUTPUT_VAR || this == UDF_VAR
                || this == UDF_DERIVED_VAR || this == GLOBAL_VAR
                || this == GLOBAL_REF_VAR;
    }
}
